package com.cwf.business.activity.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cwf.business.R;
import com.cwf.business.activity.AppActivityManager;
import com.cwf.business.activity.guide.GuideActivity1;
import com.cwf.business.activity.guide.GuideActivity2;
import com.cwf.business.preferences.PreferencesUtils;
import com.cwf.business.shaderutil.circle.CircularImageView;
import com.cwf.business.util.MainUtil;
import com.cwf.business.util.VerifyPhoneEmailUtil;
import com.cwf.business.util.ViewUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 登录页
 * Created by sam_chan on 2017/5/8.
 */

public class LoginActivity extends Activity {

    private CircularImageView LogoImageView;

    private EditText mUsernameEdit;
    private EditText mPasswordEdit;
    private CheckBox mSHPasswordImg;

    private TextView mLoginText;

    private LinearLayout mRegisterLayout;
    private LinearLayout mForgetPasslayout;


    private Dialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        showGuideActivity();
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        AppActivityManager.getAppActivityManager().addActivity(this);

        findView();
        bindData();
        setListener();

        /*final Picasso picasso = Picasso.with(getApplicationContext());
        picasso.setLoggingEnabled(true);
        picasso.setIndicatorsEnabled(false);
        picasso.load("https://farm4.staticflickr.com/3883/15068310256_891b454952_z.jpg")
                .placeholder(R.mipmap.neo)
                .into(LogoImageView);*/
    }

    private void showGuideActivity() {
        final boolean firstOpen = PreferencesUtils.getInstance().getPreferences(PreferencesUtils.FRIST_ONE_OPEN_APP, false);
        if (firstOpen) {
            PreferencesUtils.getInstance().putPreferences(PreferencesUtils.FRIST_ONE_OPEN_APP, true);
            startActivity(new Intent(LoginActivity.this, GuideActivity2.class));
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }

    private void findView() {
        LogoImageView = (CircularImageView) findViewById(R.id.image);
        mUsernameEdit = (EditText) findViewById(R.id.username_edit);
        mPasswordEdit = (EditText) findViewById(R.id.password_edit);
        mPasswordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        mSHPasswordImg = (CheckBox) findViewById(R.id.show_hint_password_img);

        mLoginText = (TextView) findViewById(R.id.login_text);
        mRegisterLayout = (LinearLayout) findViewById(R.id.register_layout);
        mForgetPasslayout = (LinearLayout) findViewById(R.id.forget_password_layout);
    }

    private void bindData() {

    }

    private void setListener() {

        /**
         * 显示/隐藏 密码
         */
        mSHPasswordImg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    //如果选中，显示密码
                    mPasswordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mPasswordEdit.setSelection(mPasswordEdit.getText().length());
                } else {
                    //否则隐藏密码
                    mPasswordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mPasswordEdit.setSelection(mPasswordEdit.getText().length());
                }

            }
        });

        /**
         * 登录
         */
        mLoginText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mUsernameEdit.getText().toString().trim();
                if ("".equals(phone)) {
                    ViewUtil.Toasts(LoginActivity.this, getResources().getString(R.string.toast_empty_phone));
                    return;
                }

                if (!VerifyPhoneEmailUtil.isMobileNO(phone)) {
                    ViewUtil.Toasts(LoginActivity.this, getResources().getString(R.string.toast_wrong_phone));
                    return;
                }

                String password = mPasswordEdit.getText().toString().trim();
                if ("".equals(password)) {
                    ViewUtil.Toasts(LoginActivity.this, getResources().getString(R.string.toast_empty_password));
                    return;
                } else if (password.length() < 6 || password.length() > 16) {
                    ViewUtil.Toasts(LoginActivity.this, getResources().getString(R.string.toast_wrong_password));
                    return;
                }

                if (MainUtil.isNetworkAvailable(LoginActivity.this)) {
                    getLogin(phone, password);
                } else {
                    ViewUtil.Toasts(LoginActivity.this, getResources().getString(R.string.network_available));
                }

                loadingDialog = ViewUtil.createLoadingDialog(LoginActivity.this, getResources().getString(R.string.wait));


            }
        });

        /**
         * 忘记密码
         */
        mForgetPasslayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        /**
         * 注册账号
         */
        mRegisterLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 联网登录
     *
     * @param phone
     * @param password
     */
    private void getLogin(String phone, String password) {

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            /*if (loadingDialog != null) {
                ViewUtil.closeDialog(loadingDialog);
                loadingDialog = null;
            } else {*/
            //指定时间内双击退出
            exitBy2Click();
            //}

            return true;
        } else if ((keyCode == KeyEvent.KEYCODE_MENU)) {

            return true;
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            ViewUtil.Toasts(this, "再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000);
        } else {
            AppActivityManager.getAppActivityManager().finishAllActivity();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
